//
//  SearchResultsViewController.m
//  muscleLife
//
//  Created by xalo on 16/5/4.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "SearchResultsViewController.h"
#import "HealthSearchModel.h"
#import "HealthDetailViewController.h"

@interface SearchResultsViewController ()<UITableViewDataSource,UITableViewDelegate>



@end

@implementation SearchResultsViewController

-(NSMutableArray *)sourceArray{

    if (!_sourceArray) {
        _sourceArray = [NSMutableArray array];
    }
    return _sourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    NSLog(@"显示我了");
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.sourceArray.count+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
        cell.textLabel.text=[NSString stringWithFormat:@"搜索：%@",self.searchStr];
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
        HealthSearchModel* model = [self.sourceArray objectAtIndex:indexPath.row - 1];
        cell.textLabel.text = model.title;

        return cell;
    }

    return nil;
}

-(void)searchDatawithkey:(NSString*)str{

    MBProgressHUD* hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    hud.labelText = @"正在拼命搜索...";
    [self.sourceArray removeAllObjects];
    [self acquireSourceDataWithKeyWord:str page:nil size:nil finished:^(MOBAResponse *response) {
           if (response.error) {

            }else{
                NSArray* listArray = response.responder[@"list"];
                for (NSDictionary *itemDic in listArray) {
                    HealthSearchModel *model=[[HealthSearchModel alloc]init];
                    [model setValuesForKeysWithDictionary:itemDic];
                    [self.sourceArray addObject:model];
                }

            }
        //代理传值
        if (self.delegate&&[self.delegate respondsToSelector:@selector(searchResultsViewController:sourceData:)]) {
            [self.delegate searchResultsViewController:self sourceData:self.sourceArray];
        }
        [hud hide:YES];
        [self.tableView reloadData];
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self dismissViewControllerAnimated:YES completion:^{
            HealthSearchModel* model = [self.sourceArray objectAtIndex:indexPath.row -1];
            self.block(model);
        }];

    }else{
        if (self.block) {
            [self dismissViewControllerAnimated:YES completion:^{
                HealthSearchModel* model = [self.sourceArray objectAtIndex:indexPath.row -1];
                self.block(model);
            }];

        }
    }

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
